package com.sjz.myfrist;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

/****
 * RecyclerView不仅可以轻松实现和ListView同样的效果，还优化了ListView中存在的各种不足，且Android官方也更加推荐使用RecyclerView。
 *   RecyclerView属于新增控件，被定义在了support库中，想要使用，需要在项目的build.gradle中添加相应的依赖库。
 *   implementation 'androidx.recyclerview:recyclerview:1.2.1'
 */
public class BasicRecyclerView1 extends AppCompatActivity {
    private List<Image> list = new ArrayList<>();


    /***
     *  首先，再onCreate方法中，创建StaggeredGridLayoutManager实例，构造函数接收两个参数，第一个参数用于指定布局的列数，第二个参数用于指定布局的排列方向。
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_recycler_view1);
        init();//初始化数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter ad = new RecyclerViewAdapter(list);
        recyclerView.setAdapter(ad);
    }
    private void init(){
        for(int i = 0; i < 5; i++){
            Image a = new Image("AAAAAAAAAAAAAAAAAA",R.mipmap.ic_launcher);
            list.add(a);
            Image b = new Image("BBBBBB",R.mipmap.ic_launcher);
            list.add(b);
            Image c = new Image("CCCCCCCCCCC",R.mipmap.ic_launcher);
            list.add(c);
            Image d = new Image("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",R.mipmap.ic_launcher);
            list.add(d);
            Image e = new Image("EEE",R.mipmap.ic_launcher);
            list.add(e);
        }
    }

    /**
     * 横向滚动
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_recycler_view1);
        init();//初始化数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//  修改成 横向滚动  并且修改对应的配置文件recycler_item2
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter ad = new RecyclerViewAdapter(list);
        recyclerView.setAdapter(ad);
    }
     **/
/****
 *  最基础用法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_recycler_view1);
        init();//初始化数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter ad = new RecyclerViewAdapter(list);
        recyclerView.setAdapter(ad);
    }
 ***/

    /*
      横向和原始调用的初始化
    private void init(){
        for(int i = 0; i < 2; i++){
            Image a = new Image("RecyclerView1A",R.mipmap.ic_launcher);
            list.add(a);
            Image b = new Image("RecyclerView1B",R.mipmap.ic_launcher);
            list.add(b);
            Image c = new Image("RecyclerView1C",R.mipmap.ic_launcher);
            list.add(c);
            Image d = new Image("RecyclerView1D",R.mipmap.ic_launcher);
            list.add(d);
            Image e = new Image("RecyclerView1E",R.mipmap.ic_launcher);
            list.add(e);
        }
    }*/
}